package him.works.trainapp;

import him.works.trainapp.Utils.ArgumentsBean;

import java.util.ArrayList;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class FrontActivity extends Activity {
	Bundle mBundleResult = null;
	PlaceholderFragment mPlaceholdfragObj = null;
	ProgressDialog mProgress;
    int mProgressStatus = 0;

    Handler mHandler = new Handler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_front);
		if (savedInstanceState == null) {
			mPlaceholdfragObj = new PlaceholderFragment();
			getFragmentManager().beginTransaction()
					.add(R.id.container, mPlaceholdfragObj).commit();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.front, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}
	
	public boolean showNoNetwork() {
		boolean isUnconnected = false;
		ConnectivityManager cm =
		        (ConnectivityManager)getSystemService(android.content.Context.CONNECTIVITY_SERVICE);
		 
		NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
		isUnconnected = (activeNetwork != null) ? !activeNetwork.isConnectedOrConnecting() : 
			true;
		if(isUnconnected) {
			Toast.makeText(this, "Hey Honey, no network :(", Toast.LENGTH_LONG).show();			
		}
		return isUnconnected;
	}

	public void launchResults(String strFromStn, String strLine,
			String strDirection, int iPickedHour, int iPickedMinute) {
		mBundleResult = new Bundle();
		ArgumentsBean argbeanObj = Utils.getArgumentBean(
				getApplicationContext(), strFromStn, strDirection, strLine,
				iPickedHour, iPickedMinute);
		new TrainDataTask().execute(argbeanObj);
	}

	public class TrainDataTask extends
			AsyncTask<ArgumentsBean, Void, ArrayList<String>> {

		@Override
		protected ArrayList<String> doInBackground(ArgumentsBean... argbeanObj) {
			return Utils.getTrainTimes(argbeanObj[0]);
		}

		@Override
		protected void onPostExecute(ArrayList<String> result) {
			if(result.isEmpty()) {
				return;
			}
			mBundleResult.putStringArrayList(Utils.NAME_RESULT_SET, result);
			super.onPostExecute(result);
			FragmentManager fragMgr = getFragmentManager();
			FragmentTransaction fragTns = fragMgr.beginTransaction();
			ResultFragment resultfragObj = new ResultFragment();
			resultfragObj.passDataToFragment(mBundleResult);
			fragTns.replace(R.id.myFrame, resultfragObj, "result");
			fragTns.commit();
		}

	}

}
